package it.zqdszb.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import it.zqdszb.R;
import it.zqdszb.entity.HotChannelBean;
import it.zqdszb.entity.PlayLinkListBean;
import it.zqdszb.library.api.HttpPostService;
import it.zqdszb.library.ui.BaseLazyMVPFragment;
import it.zqdszb.library.view.EmptyLayout;
import it.zqdszb.mvp.contract.TvRecommendContract;
import it.zqdszb.mvp.modelImp.TvRecommendModeImp;
import it.zqdszb.mvp.presenter.TvRecommendPresenter;
import it.zqdszb.ui.activity.LivePlayActivity;
import it.zqdszb.ui.adapter.TvRecommendAdapter;

/**
 * Created by GH on 2018-05-13.
 */

public class RecommendTvFragment extends BaseLazyMVPFragment<TvRecommendPresenter,TvRecommendModeImp>
		implements TvRecommendContract.TvRecommendView  {

	@BindView(R.id.emptyLayout)
	EmptyLayout emptyLayout;
	private HotChannelBean channelBean;


	public static Fragment newInstance(String title) {
		RecommendTvFragment fragment = new RecommendTvFragment();
		Bundle b = new Bundle();
		b.putSerializable("PagerTitle", title);
		fragment.setArguments(b);
		return fragment;
	}

	@Nullable
	@Override
	public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return setView(inflater,R.layout.fragment_recommend);
	}

	@Override
	protected void initView() {
		emptyLayout.showLoading();
		TvRecommendAdapter tvRecommendAdapter=new TvRecommendAdapter(R.layout.item_tv_recommend,datas,getThisActivity());
		emptyLayout.setAdapter(tvRecommendAdapter);
		tvRecommendAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
		tvRecommendAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
			@Override
			public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
				if(datas.get(position) instanceof HotChannelBean){
					channelBean = (HotChannelBean) datas.get(position);
					presenter.getPlayLink(channelBean.getChannel_id());
				}
			}
		});
	}

	@Override
	protected void onFragmentFirstVisible() {
		super.onFragmentFirstVisible();
		presenter.getData();
	}

	@Override
	public void showView(List<HotChannelBean> result) {
		emptyLayout.showView();
		datas.clear();
		Iterator<HotChannelBean> iter = result.iterator();
		while(iter.hasNext()){
			HotChannelBean tabBean = iter.next();
			if(tabBean.getCibn()==1){
				iter.remove();
			}
		}
		datas.addAll(result);
		emptyLayout.getAdapter().notifyDataSetChanged();
	}

	@Override
	public void goPlay(List<PlayLinkListBean> result) {
		Intent  intent=new Intent(getThisActivity(), LivePlayActivity.class);
		intent.putExtra("channel",channelBean);
		intent.putExtra("playLinkList", (Serializable) result);
		startActivity(intent);
	}

	@Override
	public void showError(String mothead) {
		if(mothead== HttpPostService.getChannelPlayLink){
			getThisActivity().toast("加载播放地址失败，请稍后再试");
		}
		if(mothead== HttpPostService.getLiveHotChannel){
		}
	}
}
